﻿global using FastEasy.IRepository;
global using FastEasy.Model;
global using FastEasy.Model.Models;
global using SqlSugar;
global using System.Data;
global using System.Linq.Expressions;

namespace FastEasy.Repository;

public class BaseRepository<T> : IBaseRepository<T> where T : class, new()
{
    #region 构造

    public BaseRepository(ISqlSugarClient _db)
    {
        db = _db;
    }

    #endregion 构造

    protected readonly ISqlSugarClient db;

    public virtual async Task<bool> CreateEntityAsync(T entity)
    {
        try
        {
            return await db.Insertable<T>(entity).ExecuteCommandAsync() > 0;
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<int> CreateEntitysAsync(List<T> entitys)
    {
        try
        {
            return await db.Insertable<T>(entitys).ExecuteCommandAsync();
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<bool> DeleteEntityByIdAsync(int id)
    {
        try
        {
            return await db.Deleteable<T>().In(id).ExecuteCommandAsync() > 0;
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<int> DeleteEntitysByWhereAsync(Expression<Func<T, bool>> expression)
    {
        try
        {
            return await db.Deleteable<T>().Where(expression).ExecuteCommandAsync();
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<T> GetEntityByIdAsync(int id)
    {
        try
        {
            return await db.Queryable<T>().InSingleAsync(id);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<List<T>> GetEntitysAsync()
    {
        try
        {
            return await db.Queryable<T>().ToListAsync();
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<List<T>> GetEntitysByWhereAsync(Expression<Func<T, bool>> expression)
    {
        try
        {
            return await db.Queryable<T>().Where(expression).ToListAsync();
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual List<T> GetEntitysToPage(int pageNumber, int pageSize, ref int totalCount)
    {
        try
        {
            return db.Queryable<T>().ToPageList(pageNumber, pageSize, ref totalCount);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<bool> UpdateEntityAsync(T entity)
    {
        try
        {
            return await db.Updateable<T>(entity).ExecuteCommandAsync() > 0;
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<int> UpdateEntitysAsync(List<T> entitys)
    {
        try
        {
            return await db.Updateable<T>(entitys).ExecuteCommandAsync();
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<List<T>> GetEntitysBySqlListAsync(string sql)
    {
        try
        {
            return await db.Ado.SqlQueryAsync<T>(sql);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<DataTable> GetEntitysBySqlDatatableAsync(string sql)
    {
        try
        {
            return await db.Ado.GetDataTableAsync(sql);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public virtual async Task<DataTable> UseProcAsync(string procName, object parameters)
    {
        try
        {
            return await db.Ado.UseStoredProcedure().GetDataTableAsync(procName, parameters);
        }
        catch (Exception)
        {
            throw;
        }
    }

    public void CreateModels(string name = "1")
    {
        if (name == "1")
        {
            db.DbFirst.IsCreateAttribute().CreateClassFile(@"A:\FastEasy\API\FastEasy.Model\Models\", "FastEasy.Model.Models");
        }
        else
        {
            db.DbFirst.IsCreateAttribute().Where(name).CreateClassFile(@"A:\FastEasy\API\FastEasy.Model\Models\", "FastEasy.Model.Models");
        }
    }
}